package Servlet;


import Pojo.Msgpojo;
import Pojo.UserMsgpojo;
import Service.Serviceimpl.UserMsgServiceimpl;
import com.google.gson.Gson;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Objects;
//用户层获取公告信息还有添加用户信息
public class UserMsgServlet extends HttpServlet {
    UserMsgServiceimpl userMsgServiceimpl =new UserMsgServiceimpl();
    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        req.setCharacterEncoding("UTF-8");
        String code=req.getParameter("code");
        String name=req.getParameter("name");
        int age = Integer.parseInt(req.getParameter("age"));
        String checkstatus = (req.getParameter("ma"));
        if (Objects.equals(checkstatus, "0")){
            checkstatus="绿码";
        }else if (Objects.equals(checkstatus, "1")){
            checkstatus="黄码";
        }else {
            checkstatus="红码";
        }

        String state = req.getParameter("switch");
        state=state=="no"?"已接种":"未接种";
        String sex = req.getParameter("sex");
        UserMsgpojo msgpojo = new UserMsgpojo(name, age, checkstatus, state, sex);


        boolean b = userMsgServiceimpl.AddusermsgService(msgpojo);
//        管理员添加的成功返回ok
        if (b&& code.equals("1")){
            resp.getWriter().write("ok");
        }
        else if (b&&code.equals("0")){
            resp.sendRedirect("/web/indextwo.jsp?show=0");
//            req.getRequestDispatcher("/web/index two.html").forward(req,resp);
        }else resp.sendRedirect("/web/indextwo.jsp?show=1");
    }


    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        resp.setContentType("text/html;charset=UTF-8");
        List<Msgpojo> msgpojos = userMsgServiceimpl.GetMsg();
//        System.out.println(msgpojos.size());
        Map<String,Object> map = new HashMap<String,Object>();
        map.put("code",0);
        map.put("msg","获取数据成功");
        map.put("data",msgpojos);
        map.put("count",msgpojos.size());
        Gson gson = new Gson();
        String s = gson.toJson(map);
        resp.getWriter().write(s);
    }
}
